# coding=utf-8
import re
import socket
import sys
import urllib
import threading


def download(url,filename):
    ''' download mp3 file '''
    print (url)
    errno = None
    try:
        urllib.urlretrieve(url, filename)
    except socket.error:
        errno = sys.exc_info()[0]
    if errno == socket.timeout:
        print ("There was a timeout")

class DownloadThread(threading.Thread):
    ''' dowanload thread class '''
    def __init__(self,url,savePath):
        threading.Thread.__init__(self)
        self.url = url
        self.savePath = savePath

    def run(self):
        download(self.url,self.savePath)




html = """<!DOCTYPE html>
<html lang="en">
<head>

<script language="javascript">
if(top==self)top.location="http://www.psttt.com/html/1836.html"
</script>

<TITLE></TITLE>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link href="jplayer/play.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="javascript" src="jplayer/jquery.min.js"></script>
<script type="text/javascript" src="jplayer/jquery.jplayer.min.js"></script>

<script>
//var searchString = location.search
//var pos = searchString.indexOf ("?")
//if (pos == 0) {url = searchString.substr (1, searchString.length)}
//url2=url.split('$$$');
//document.write(''+url2[1]+' '+url2[2]+'');

 var nexturl;
 var preurl;
 var url6489481;
 var murl6489481;
nexturl = '1836_49_1_2.html';
preurl = '1836_49_1_0.html';
murl6489481 = '.mp3?';
url6489481 = '';

function next()
	{
		if ((nexturl == 'nextnan') || (preurl == 'prenan'))
		{
			alert("已达到最后一集!");		
		}else
		{
			window.parent.location.href=nexturl;
		}
	}
	function pre()
	{
		if ((preurl == 'prenan') || (nexturl == 'nextnan'))
		{
			alert("已达到第一集!");
		}else
		{
			window.parent.location.href=preurl;
		}

	}


$(document).ready(function(){
	$("#jquery_jplayer_1").jPlayer({
		ready: function (event) {
			$(this).jPlayer("setMedia", {
								mp3:'http://177e.jqkan.com:8000/玄幻小说/2009/凡人修仙传/凡人修仙传001'+murl6489481+'5804026551697x1526408652x5804321164451-14345637e4e0f44515190f'
			}).jPlayer("play"); 
		},swfPath: "jplayer",
		supplied: "mp3",
		wmode: "window"
		,
ended: function(event) {  
  if(event.jPlayer.options.loop) { 
     $(this).jPlayer("play"); 
} else {    
next();
}
}"""


str2 = re.compile( "\"setMedia\",{([\s\S]*?)}").findall(html.replace(' ', ''))[0]

urlList = re.compile("\+([\s\S]*?)\+").findall(str2)

sprUrl = re.compile(urlList[0]+" = '([\s\S]*?)'").findall(html)

content_url = re.compile("mp3:'([\s\S]*?)'").findall(str2.split("+")[0])[0]

if content_url.endswith("+'"):
    content_url = sprUrl[1]
elif not content_url.endswith(".mp3"):
    content_url = content_url+ sprUrl[0]

if content_url.endswith("?"):
    content_url = content_url.split("?")[0]

file_name = content_url.split("/")[-1]

print ('start')
print ("C:/mp3/"+file_name)

download("http://177e.jqkan.com:8000/玄幻小说/2009/凡人修仙传/凡人修仙传1491.mp3",u"C:/mp3/凡人修仙传001.mp3")